package com.zg.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zg.pojo.User;
import com.zg.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	
	@RequestMapping("/showUser")
	public String toIndex(HttpServletRequest request,Model model){
		return "user/showUser";
	}
	
	@RequestMapping(value="/addUser", method=RequestMethod.GET)
	public String addUserForm(HttpServletRequest request,Model model){
		model.addAttribute("user", "get");
		return "user/addUser";
		
	}
	
	@RequestMapping(value="/addUser", method=RequestMethod.POST)
	public String addUser(HttpServletRequest request,Model model){
		String name =new String(request.getParameter("name"));
		String password = new String(request.getParameter("pwd"));
		String role = request.getParameter("role");
		int auth = 0;
		if(role.equals("admin")) {
			auth = 7;
		}
		if(role.equals("operative")) {
			auth = 2;
		}
		if(role.equals("provider")) {
			auth = 1;
		}
		User user = new User();
		System.out.println("HHH" + name);
		user.setName(name);
		user.setAuth(auth);
		user.setPassword(password);
		
		this.userService.insert(user);
		
		return "redirect:/user/showUser";
	}
}
